﻿Public Class HomeControl
    Friend Dict As Dictionary(Of String, Integer)
    Private Sub JoinButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles JoinButton.Click
        If Not GameList.SelectedIndex = -1 Then
            MainForm.GameID = Dict.Item(GameList.SelectedItem)
            MainForm.CurrentGameName = GameList.SelectedItem
            MPQClient.Server.RegisterClient(MainForm.GameID)
            DisableButtons()
        End If
    End Sub

    Private Sub HomeControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        MPQClient.Server.GetGames()
        Dim UserControl As New UserControl
        UserControl.Name = "UserControl"
        UserControl.UsernameLabel.Text = Username

        MainForm.ScoreboardPanel.Controls.RemoveByKey("SplashControl")
        MainForm.ScoreboardPanel.Controls.Add(UserControl)
        MainForm.ScoreboardPanel.Controls("UserControl").BringToFront()

        If FirstPlay Then
            TutorialControl1.ShowAndMoveOn()
        End If
    End Sub

    Private Sub CreateButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CreateButton.Click
        DisableButtons()
        Dim name As String = ""

        'NB: Test Lines
        With My.Application.CommandLineArgs
            If .Contains("/DEBUG") Then
                name = .Item(.IndexOf("/DEBUG") + 1)
            Else
                name = InputBox("Enter Game Name")
            End If
        End With

        If name = "" Then
            MsgBox("Please enter a name")
            EnableButtons()
            Exit Sub
        End If
generateID: Dim ID As Integer = (New Random).Next
        If Dict.Values.Contains(ID) Then
            GoTo generateID
        End If
        MPQClient.Server.CreateGame(ID, name)
        System.Threading.Thread.Sleep(500)
        MainForm.GameID = ID
        MainForm.CurrentGameName = name
        MPQClient.Server.RegisterClient(MainForm.GameID)
    End Sub

    Private Sub RefreshButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshButton.Click
        MPQClient.Server.GetGames()
        DisableButtons()
    End Sub

    Public Sub EnableButtons()
        RefreshButton.Enabled = True
        JoinButton.Enabled = True
        CreateButton.Enabled = True
    End Sub

    Public Sub DisableButtons()
        RefreshButton.Enabled = False
        JoinButton.Enabled = False
        CreateButton.Enabled = False
    End Sub

    Private Sub ScoresUpdateTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ScoresUpdateTimer.Tick
        Try
            MPQClient.Server.UpdateTeamScores()
            MPQClient.Server.UpdateUserScores()
        Catch
        End Try
    End Sub

    Private Sub CreateNewTeamItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreateNewTeamItem.Click
        MPQClient.Server.CreateTeam(InputBox("Enter new team name"), MPQClient.Username)
        MPQClient.Server.GetUserDetails(Username)
    End Sub

    Private Sub JoinTeamItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles JoinTeamItem.Click
        If MsgBox("Are you sure you would like to join " & TeamScoresView.SelectedItems(0).SubItems(0).Text & "?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            MPQClient.Server.JoinTeam(TeamScoresView.SelectedItems(0).SubItems(0).Text, MPQClient.Username)
            MPQClient.Server.GetUserDetails(Username)
        End If
    End Sub
End Class
